package homework4;

import java.io.*;
import java.util.StringTokenizer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame
{
	private static Dimension defaultSize;
	private static Point defaultLocation;
	private int[] inputArr;
	private String input;
	private String result;
	JPanel textPanel, runPanel;
	JTextArea inputArea, outputArea;
	JButton runButton, clearButton;

	static
	{
		int x = Toolkit.getDefaultToolkit().getScreenSize().width;
		int y = Toolkit.getDefaultToolkit().getScreenSize().height;

		// Set default size as the 4/5 of screen size
		defaultSize = new Dimension((x * 1) / 2, (y * 1) / 2);

		// Set the default location in the center of screen
		defaultLocation = new Point(x / 2 - defaultSize.width / 2, y / 2
				- defaultSize.height / 2);
	}

	public MainFrame()
	{
		super("Longest Incrasing Sequence Demo (by @lianghao.lee)");
		init();
	}

	private void init()
	{
		setLayout(new BorderLayout());

		/******** Text Panel *********/
		textPanel = new JPanel();
		textPanel.setLayout(new GridLayout(2, 1, 5, 5));

		// Input textArea
		inputArea = new JTextArea(
				"***Please input integer numbers below(space as delim):***\n");
		inputArea.setEditable(true);
		inputArea.setFont(new Font("Courier New", Font.PLAIN, 16));
		textPanel.add(inputArea);

		// Output textArea
		outputArea = new JTextArea("Result Console:\n");
		outputArea.setEditable(false);
		outputArea.setFont(new Font("Courier New", Font.PLAIN, 16));
		textPanel.add(outputArea);

		// register textPanel
		add(textPanel, BorderLayout.CENTER);

		/******** Run Panel *********/
		runPanel = new JPanel();

		// runButton and its actionListener
		runButton = new JButton("Run");
		runButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				BufferedReader reader = new BufferedReader(new StringReader(
						inputArea.getText()));
				try
				{
					String info = reader.readLine();

					// if user changes the hints or input integer after hints
					// immediately
					if (!info
							.equals("***Please input integer numbers below(space as delim):***"))
					{
						JOptionPane.showMessageDialog(null,
								"Please input integer below hints!");
						clear();
						return;
					}

					// if user forget input
					input = reader.readLine();
					if (input == null)
					{
						JOptionPane.showMessageDialog(null,
								"Please input data!");
						clear();
						return;
					}

					// get input
					StringTokenizer stk = new StringTokenizer(input);
					inputArr = new int[stk.countTokens()];
					for (int i = 0; i < inputArr.length; i++)
					{
						inputArr[i] = Integer.parseInt(stk.nextToken());
					}

					// run LIS
					LIS lis = new LIS();
					int[] indexer = new int[inputArr.length + 1];
					indexer[0] = -1;
					int[] prefix = new int[inputArr.length];
					for (int i = 0; i < prefix.length; i++)
					{
						prefix[i] = -1;
					}
					int len = lis.calcLIS(inputArr, prefix, indexer);
					result = lis.printLIS(inputArr, prefix, len, indexer[len]);

					// output result
					outputArea.setText(result);
				}
				catch (NumberFormatException nfe)
				{
					// if the user input is not integers
					JOptionPane.showMessageDialog(null,
							"Input should be Integer!");
					clear();
				}
				catch (IOException e)
				{
					// if other exception occur
					JOptionPane.showMessageDialog(null, e.getMessage());
					clear();
				}
			}
		});
		runPanel.add(runButton);

		// Clear button and its listener
		clearButton = new JButton("Clear");
		clearButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				clear();
			}
		});
		runPanel.add(clearButton);

		// register runPanel
		runPanel.setBackground(Color.gray);
		add(runPanel, BorderLayout.SOUTH);

		// set default properties
		setSize(defaultSize);
		setLocation(defaultLocation);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	private void clear()
	{
		inputArea
				.setText("***Please input integer numbers below(space as delim):***\n");
		outputArea.setText("Result Console:\n");
	}

	public static void main(String[] args)
	{
		MainFrame frame = new MainFrame();
	}
}
